package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.GpsClientStateDao;
import cn.topevery.garden.entity.po.Extent;
import cn.topevery.garden.entity.po.GpsClientState;
import cn.topevery.garden.entity.query.GpsClientStateQuery;
import cn.topevery.garden.entity.vo.GpsClientStateVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 状态详情
 *
 * @author code-generate
 * @date 2021-09-13
 */
@Repository
public class GpsClientStateDaoImpl extends BaseDaoImpl<GpsClientState> implements GpsClientStateDao {

    @Override
    public <P extends DbCsp> List<GpsClientState> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<GpsClientStateVO> listPage(String name, GpsClientStateQuery query) {

        String table = "v_gps_client_state tgcs";

        String columns = "tgcs.*,'" + name + "' as c_name";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tgcs.c_id desc");
        sqlQueryMate
//                .where(true, "tgcs.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(query.getId() != null, "tgcs.c_obj_id", ScriptConditionEnum.Eq, query.getId())
                .where(query.getObjType() != null, "tgcs.c_obj_type", ScriptConditionEnum.Eq, query.getObjType())
                .where(StringUtils.isNotBlank(query.beginDate), "tgcs.c_db_create_date", ScriptConditionEnum.GreaterThanEq, query.beginDate)
                .where(StringUtils.isNotBlank(query.endDate), "tgcs.c_db_create_date", ScriptConditionEnum.LessThanEq, query.endDate);

        return getDataPager(sqlQueryMate, pageSize, pageIndex, GpsClientStateVO.class);
    }

    @Override
    public List<Map<String, Object>> getObjCurrentPos(GpsClientStateQuery query) {
        Map<String, Object> map = new HashMap<String, Object>(){
            {
                put("p_obj_id", query.getObjId());
                put("p_obj_type", query.getObjType());
            }
        };
        return getMapListForProcedure("p_get_obj_current_pos", map);
    }

    @Override
    public Extent getExtentByTypeAndId(GpsClientStateQuery query) {
        StringBuilder table = new StringBuilder("t_extent te ");

        if (query.getObjType() == 0) {
            table.append("join t_person tp on te.c_id = tp.c_extent_id");
        } else {
            table.append("join t_car tc on te.c_id = tc.c_extent_id");
        }

        String columns = "te.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table.toString()).setColumns(columns);
        sqlQueryMate
                .where(true, "te.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, query.getObjType() == 0 ? "tp.c_user_id" : "tc.c_car_id", ScriptConditionEnum.Eq, query.getObjId());
        AtomicReference<Extent> extent = new AtomicReference<>(new Extent());
        run(sqlQueryMate.getSql(), CommandType.Text, dbCommand -> {
            sqlQueryMate.getParameters().forEach(dbCommand::addParameter);
            extent.set(dbCommand.executeToEntity(Extent.class));
        });
        return extent.get();
    }

    @Override
    public List<List<Map<String, Object>>> getGpsTrace(GpsClientStateQuery query) {
        Map<String, Object> map = new HashMap<String, Object>(){
            {
                put("p_obj_id", query.getObjId());
                put("p_begin_date", query.beginDate);
                put("p_end_date", query.endDate);
                put("p_obj_type", query.getObjType());
            }
        };
        return getMapListsForProcedure("p_fetch_obj_gps_trace", map);
    }

    @Override
    public List<List<Map<String, Object>>> getGpsTraceSprinkler(GpsClientStateQuery query) {
        Map<String, Object> map = new HashMap<String, Object>(){
            {
                put("p_obj_id", query.getObjId());
                put("p_begin_date", query.beginDate);
                put("p_end_date", query.endDate);
            }
        };
        return getMapListsForProcedure("p_fetch_obj_gps_trace_sprinkler", map);
    }
}
